<!DOCTYPE html>
<html lang="zh" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="book">
<meta name="generator" content="Paradox, paradox-material-theme=0.6.0, mkdocs-material=3.0.3">

<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">


<meta name="description" content="book">
<link rel="shortcut icon" href="../assets/images/favicon.png">
<title>自定义指令 · Scala Web 开发——基于Akka HTTP</title>
<link rel="stylesheet" href="../assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="../assets/stylesheets/application-palette.22915126.css">
<meta name="theme-color" content="#009688" />
<link rel="stylesheet" href="../lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="../lib/prettify/prettify.css">
<script src="../assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="../assets/fonts/font-awesome.css">
<link rel="stylesheet" href="../assets/fonts/material-icons.css">
<link rel="stylesheet" href="../assets/stylesheets/paradox-material-theme.css">
</head>
<body
data-md-color-primary="teal"
data-md-color-accent="indigo"
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../index.html" title="Scala Web 开发——基于Akka HTTP" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
Scala Web 开发——基于Akka HTTP
</span>
<span class="md-header-nav__topic">
自定义指令
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">&#xE5CD;</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>

</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/yangbajing/scala-web-development"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
yangbajing/scala-web-development
</div>
</a>

</div>
</div>
</div>
</nav>
</header>

<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="../index.html" title="Scala Web 开发——基于Akka HTTP" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="../index.html" title="Scala Web 开发——基于Akka HTTP">
Scala Web 开发——基于Akka HTTP
</a>
</label>
<div class="md-nav__source">
<a href="https://github.com/yangbajing/scala-web-development"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
yangbajing/scala-web-development
</div>
</a>

</div>
<ul>
  <li><a href="../preface.html" class="page">前言</a></li>
  <li><a href="../env/index.html" class="page">Scala 环境配置</a>
  <ul>
    <li><a href="../env/env.1.html" class="page">Sbt</a></li>
    <li><a href="../env/env.2.html" class="page">IDE开发工具</a></li>
    <li><a href="../env/env.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../scala/index.html" class="page">Scala 语言基础</a>
  <ul>
    <li><a href="../scala/scala.0.html" class="page">REPL</a></li>
    <li><a href="../scala/scala.1.html" class="page">你好，Scala</a></li>
    <li><a href="../scala/scala.2.html" class="page">Scala基础</a></li>
    <li><a href="../scala/scala.3.html" class="page">流程和函数</a></li>
    <li><a href="../scala/scala.4.html" class="page">集合</a></li>
    <li><a href="../scala/scala.5.html" class="page">class和object</a></li>
    <li><a href="../scala/scala.6.html" class="page">函数式</a></li>
    <li><a href="../scala/scala.7.html" class="page">Trait</a></li>
    <li><a href="../scala/scala.8.html" class="page">并发</a></li>
    <li><a href="../scala/scala.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../basic/index.html" class="page">Akka HTTP 基础</a>
  <ul>
    <li><a href="../basic/basic.0.html" class="page">Akka HTTP 基础</a></li>
    <li><a href="../basic/basic.1.html" class="page">Web 工作方式</a></li>
    <li><a href="../basic/basic.2.html" class="page">使用 Akka Http 搭建一个简单的 Web 服务</a></li>
    <li><a href="../basic/basic.3.html" class="page">Akka HTTP 的通用抽象</a></li>
    <li><a href="../basic/basic.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../server-api/index.html" class="page">服务端API</a>
  <ul>
    <li><a href="../server-api/work.html" class="page">Akka HTTP 如何使得 Web 工作</a></li>
    <li><a href="../server-api/advanced.html" class="page">高级服务端 API</a></li>
  </ul></li>
  <li><a href="../routing-dsl/index.html" class="page">路由DSL</a>
  <ul>
    <li><a href="../routing-dsl/route.html" class="page">Route 路由</a></li>
    <li><a href="../routing-dsl/directive.html" class="page">Directive 指令</a></li>
    <li><a href="../routing-dsl/custom-directive.html" class="active page">自定义指令</a></li>
    <li><a href="../routing-dsl/rejections.html" class="page">拒绝 rejections</a></li>
    <li><a href="../routing-dsl/exception.html" class="page">异常处理</a></li>
    <li><a href="../routing-dsl/file-upload.html" class="page">实战：大文件断点上传、下载和秒传</a></li>
  </ul></li>
  <li><a href="../directives/index.html" class="page">常用指令</a>
  <ul>
    <li><a href="../directives/path.html" class="page">PathDirectives（路径指令）</a></li>
    <li><a href="../directives/method.html" class="page">directives/method.html</a></li>
    <li><a href="../directives/parameter_form.html" class="page">directives/parameter_form.html</a></li>
    <li><a href="../directives/marshalling.html" class="page">directives/marshalling.html</a></li>
    <li><a href="../directives/file.html" class="page">directives/file.html</a></li>
    <li><a href="../directives/cookie.html" class="page">directives/cookie.html</a></li>
  </ul></li>
  <li><a href="../data/index.html" class="page">数据</a>
  <ul>
    <li><a href="../data/data.0.html" class="page">数据</a></li>
    <li><a href="../data/data.1.html" class="page">JSON</a></li>
    <li><a href="../data/data.ant-design-pro.html" class="page">实战：为Ant Design Pro提供后端接口</a></li>
    <li><a href="../data/data.kryo.html" class="page">Kryo</a></li>
    <li><a href="../data/data.2.html" class="page">Protobuf</a></li>
    <li><a href="../data/data.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../test/index.html" class="page">测试</a>
  <ul>
    <li><a href="../test/test.0.html" class="page">测试</a></li>
    <li><a href="../test/test.1.html" class="page">Scalatest</a></li>
    <li><a href="../test/test.2.html" class="page">测试异步代码</a></li>
    <li><a href="../test/test.3.html" class="page">端到端测试Route</a></li>
    <li><a href="../test/test.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../actor/index.html" class="page">Akka Actor</a>
  <ul>
    <li><a href="../actor/actor.html" class="page">Akka Typed Actor</a></li>
    <li><a href="../actor/actor-test.html" class="page">Akka Actor 测试</a></li>
    <li><a href="../actor/actor.z.html" class="page">Actor小结</a></li>
  </ul></li>
  <li><a href="../oauth/index.html" class="page">实战：实现OAuth 2服务</a>
  <ul>
    <li><a href="../oauth/oauth.0.html" class="page">实战：OAuth 2 服务</a></li>
    <li><a href="../oauth/oauth.1.html" class="page">OAuth 2简介</a></li>
    <li><a href="../oauth/oauth.2.html" class="page">OAuth 2接口设计</a></li>
    <li><a href="../oauth/oauth.3.html" class="page">OAuth 2服务实现</a></li>
    <li><a href="../oauth/oauth.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../database/index.html" class="page">访问数据库</a>
  <ul>
    <li><a href="../database/database.0.html" class="page">访问数据库</a></li>
    <li><a href="../database/database.1.html" class="page">使用 JDBC 访问 PostgreSQL</a></li>
    <li><a href="../database/database.2.html" class="page">使用 Slick 访问数据库</a></li>
    <li><a href="../database/database.3.html" class="page">访问 Cassandra 数据库</a></li>
    <li><a href="../database/database.4.html" class="page">访问 Redis</a></li>
    <li><a href="../database/database.5.html" class="page">访问 Elasticsearch</a></li>
    <li><a href="../database/database.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../engineering/index.html" class="page">工程化</a>
  <ul>
    <li><a href="../engineering/swagger.html" class="page">使用Swagger编写API文档</a></li>
    <li><a href="../engineering/guice.html" class="page">使用Guice管理类依赖</a></li>
  </ul></li>
  <li><a href="../grpc/index.html" class="page">Akka gRPC</a>
  <ul>
    <li><a href="../grpc/grpc.html" class="page">gRPC服务</a></li>
    <li><a href="../grpc/build-tool.html" class="page">构建工具</a></li>
    <li><a href="../grpc/deployment.html" class="page">部署</a></li>
    <li><a href="../grpc/grpc.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../config-discovery/index.html" class="page">实战：配置管理、服务发现系统</a></li>
  <li><a href="../appendix/index.html" class="page">附录</a>
  <ul>
    <li><a href="../appendix/appendix.0.html" class="page">参考资料</a></li>
    <li><a href="../appendix/appendix.1.html" class="page">专业术语</a></li>
    <li><a href="../appendix/appendix.2.html" class="page">词汇表</a></li>
  </ul></li>
  <li><a href="../donate.html" class="page">赞助</a></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../routing-dsl/custom-directive.html#自定义指令" class="header">自定义指令</a>
  <ul>
    <li><a href="../routing-dsl/custom-directive.html#命名配置" class="header">命名配置</a></li>
    <li><a href="../routing-dsl/custom-directive.html#转换已存在的指令" class="header">转换已存在的指令</a></li>
    <li><a href="../routing-dsl/custom-directive.html#从头开始实现一个指令" class="header">从头开始实现一个指令</a></li>
  </ul></li>
</ul>
</nav>

</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 1.0.0
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../routing-dsl/custom-directive.html#自定义指令" class="header">自定义指令</a>
  <ul>
    <li><a href="../routing-dsl/custom-directive.html#命名配置" class="header">命名配置</a></li>
    <li><a href="../routing-dsl/custom-directive.html#转换已存在的指令" class="header">转换已存在的指令</a></li>
    <li><a href="../routing-dsl/custom-directive.html#从头开始实现一个指令" class="header">从头开始实现一个指令</a></li>
  </ul></li>
</ul>
</nav>

</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#自定义指令" name="自定义指令" class="anchor"><span class="anchor-link"></span></a>自定义指令</h1>
<p>有3种创建自定义指令的基本方法：</p>
<ol>
  <li>将已有指令通过命名配置（比如通过组合的方式）的方式来定义新的指令</li>
  <li>转换已存在的指令</li>
  <li>从头开始实现一个指令</li>
</ol>
<h2><a href="#命名配置" name="命名配置" class="anchor"><span class="anchor-link"></span></a>命名配置</h2>
<p>创建自定义指令最简便的方法就是将一个或多个已有指令通过配置的方式分配一个新的名字来定义。事实上Akka HTTP预定义的大多数指令都由以较低级别指令命名配置的方式来定义的。如：</p>
<pre class="prettyprint"><code class="language-scala">val getPut = get &amp; put

def postEntity[T](um: FromRequestUnmarshaller[T]): Directive1[T] = post &amp; entity(um)

def completeOk: Route = complete(HttpEntity.Empty)

def completeNotImplemented: Route = complete(StatusCodes.NotImplemented)
</code></pre>
<h2><a href="#转换已存在的指令" name="转换已存在的指令" class="anchor"><span class="anchor-link"></span></a>转换已存在的指令</h2>
<p>第二种方式是通过“转换方法”来转换现有指令，这是在<code>Directive</code>类上定义的方法：</p>
<ul>
  <li><code>map/tmap</code></li>
  <li><code>flatMap/tflatMap</code></li>
  <li><code>require/trequire</code></li>
  <li><code>recover/recoverPF</code></li>
</ul>
<h3><a href="#map-tmap" name="map-tmap" class="anchor"><span class="anchor-link"></span></a>map、tmap</h3>
<p><code>map、tmap</code>就和Scala集合库上的<code>map</code>转换类似，它可以将值映射转换成另一个值。<code>map</code>用于<code>Directive1</code>类型的指令（单值指令），而<code>tmap</code>用于值为其它元组的情况，它的签名如下：</p>
<pre class="prettyprint"><code class="language-scala">def tmap[R](f: L =&gt; R): Directive[Out]
</code></pre>
<p><code>tmap</code>可以用来将提取的元组转换成另一个元组，提取的数量和类型都可以改变，而<code>map</code>只用改变变换后的类型。如下是一个虚构的例子：</p>
<pre class="prettyprint"><code class="language-scala">val twoIntParameters: Directive[(Int, Int)] =
  parameters((&quot;a&quot;.as[Int], &quot;b&quot;.as[Int]))

val myDirective: Directive1[String] =
  twoIntParameters.tmap {
    case (a, b) =&gt; (a + b).toString
  }

// tests:
Get(&quot;/?a=2&amp;b=5&quot;) ~&gt; myDirective(x =&gt; complete(x)) ~&gt; check {
  responseAs[String] shouldBe &quot;7&quot;
}
</code></pre>
<h3><a href="#flatmap-tflatmap" name="flatmap-tflatmap" class="anchor"><span class="anchor-link"></span></a>flatMap、tflatMap</h3>
<p>通过<code>map、tmap</code>可以将指令提取的值转换成其它值，但不能改变其“提取”的性质。当需要提取一个对它做一些转换操作，并将结果交给一个嵌套的指令使用时，<code>map、tmap</code>就无能为力了。同<code>map、tmap</code>类似，<code>flatMap</code>也是用于单值指令，而<code>tflatMap</code>用于其它元组值。<code>tflatMap</code>的函数签名如下：</p>
<pre class="prettyprint"><code class="language-scala">def tflatMap[R: Tuple](f: L =&gt; Directive[R]): Directive[R]
</code></pre>
<p>可以看一个例子，预定义的<code>method</code>指令，它的定义如下：</p>
<pre class="prettyprint"><code class="language-scala">def method(httpMethod: HttpMethod): Directive0 =
  extractMethod.flatMap[Unit] {
    case `httpMethod` =&gt; pass
    case _            =&gt; reject(MethodRejection(httpMethod))
  } &amp; cancelRejections(classOf[MethodRejection])

val get: Directive0 = method(HttpMethods.GET)
val post: Directive0 = method(HttpMethods.POST)
</code></pre>
<ol>
  <li>通过调用<code>extractMethod</code>指令获取请求的HTTP方法，再通过<code>flatMap[Unit]</code>转换方法对它进行处理。因为<code>extractMethod</code>是一个单值指令且转换后值为<code>Unit</code>（也是个单值），这里调用<code>flatMap</code>方法。</li>
  <li>当请求的实际HTTP方法与传入参数<code>httpMethod</code>匹配时，调用<code>pass</code>指令使其通过，否则调用<code>reject(MethodRejection(httpMethod))</code>拒绝。</li>
</ol>
<h3><a href="#require-trequire" name="require-trequire" class="anchor"><span class="anchor-link"></span></a>require、trequire</h3>
<p>require方法将单个指令转换为没有提取值的指令，该指令根据谓词函数过滤请求，所有谓词函数调用后为false的请求都被拒绝，其它请求保持不变。它的定义如下：</p>
<pre class="prettyprint"><code class="language-scala">def require(predicate: T =&gt; Boolean, rejections: Rejection*): Directive0 =
  underlying.filter(predicate, rejections: _*).tflatMap(_ =&gt; Empty)
</code></pre>
<p>从定义可以看出，它实际上是先通过谓词函数调用<code>filter</code>方法对请求进行过滤，然后再调用<code>tflatMap</code>函数将指令提取的值去掉。</p>
<h3><a href="#recover-recoverpf" name="recover-recoverpf" class="anchor"><span class="anchor-link"></span></a>recover、recoverPF</h3>
<p>recover方法允许“捕获”由底层指令向上冒泡产生的rejections，并生成且有相同提取类型的替代指令。这样就可以恢复指令来通过而不是拒绝它。它们的定义分别如下：</p>
<pre class="prettyprint"><code class="language-scala">def recover[R &gt;: L: Tuple](recovery: immutable.Seq[Rejection] =&gt; Directive[R]): Directive[R] =
  Directive[R] { inner =&gt; ctx =&gt;
    import ctx.executionContext
    @volatile var rejectedFromInnerRoute = false
    tapply({ list =&gt; c =&gt; rejectedFromInnerRoute = true; inner(list)(c) })(ctx).fast.flatMap {
      case RouteResult.Rejected(rejections) if !rejectedFromInnerRoute =&gt; recovery(rejections).tapply(inner)(ctx)
      case x =&gt; FastFuture.successful(x)
    }
  }

def recoverPF[R &gt;: L: Tuple](recovery: PartialFunction[immutable.Seq[Rejection], Directive[R]]): Directive[R] =
  recover { rejections =&gt; recovery.applyOrElse(rejections, (rejs: Seq[Rejection]) =&gt; RouteDirectives.reject(rejs: _*)) }
</code></pre>
<h2><a href="#从头开始实现一个指令" name="从头开始实现一个指令" class="anchor"><span class="anchor-link"></span></a>从头开始实现一个指令</h2>
<p>可以通过调用<code>Directive.apply</code>或它的子类型来从头开始定义一个指令，<code>Directive</code>的简化定义看起来像下面这样：</p>
<pre class="prettyprint"><code class="language-scala">abstract class Directive[L](implicit val ev: Tuple[L]) {
  def tapply(f: L =&gt; Route): Route
}

object Directive {

  /**
   * Constructs a directive from a function literal.
   */
  def apply[T: Tuple](f: (T =&gt; Route) =&gt; Route): Directive[T] =
    new Directive[T] { def tapply(inner: T =&gt; Route) = f(inner) }

}
</code></pre>
<p><code>Directive</code>类型有一个抽象方法<code>tapply</code>，参数<code>f</code>是一个函数类型，将类型<code>L</code>传入并返回<code>Route</code>。<code>Directive</code>的伴身对象提供了<code>apply</code>函数来实现自定义指令，它的参数是一个高阶函数<code>(T =&gt; Route) =&gt; Route</code>，就像小括号那样，我们应把<code>（T =&gt; Route)</code>看成一个整体，它是函数参数，返回类型为<code>Route</code>。</p>
<p><code>f</code>为我们自定义指令用于从<code>RequestContext</code>里提取值（值的类型为<code>Tuple[L]</code>），而<code>inner</code>就是<code>f</code>提取值后调用的嵌套路由，在调用<code>inner</code>时将提取出的值作为参数传入。</p>
<p>对于一个提取访问host和port的指令，可以这样实现：</p>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/yangbajing/scala-web-development/tree/master/book/src/main/scala/book/example/route/RouteExample.scala#L100-L110" target="_blank" title="Go to snippet source"></a><code class="language-scala">def hostnameAndPort: Directive[(String, Int)] = Directive[(String, Int)] {
  inner =&gt;
    ctx =&gt;
      // inner: (String, Int) =&gt; Route
      // ctx: RequestContext

      val authority: Uri.Authority = ctx.request.uri.authority
      val tupleValue: (String, Int) = (authority.host.address(), authority.port)
      val route: Route = inner(tupleValue)
      route(ctx) // Future[RouteResult]
}</code></pre>
<p>让我们来分析下这个例子：</p>
<ol>
  <li>首先是<code>hostnameAndPort</code>指令的类型<code>Directive[(String, Int)]</code>，它从请求上下文（<code>RequestContext</code>）中提取出的值是<code>Tuple2[String, Int]</code>。</li>
  <li><code>apply</code>方法执行的代码参数是：<code>inner =&gt; ctx =&gt; ....</code>其实可以看成：<code>inner =&gt; ((ctx: RequestContext) =&gt; Future[RouteResult])</code>，<code>inner</code>就是<code>f</code>函数参数<code>(T =&gt; Route）</code>部分。</li>
  <li><code>inner(tupleValue)</code>执行后结果<code>route</code>的类型是<code>Route</code>，这时这段代码为的类型就为<code>inner =&gt; ctx =&gt; Route</code>，而实际上<code>Directive.apply</code>需要的参数类型为<code>inner =&gt; Route</code>。之前我们知道，<code>Route</code>是一个类型别名<code>RequestContext =&gt; Future[RouteResult]</code>，所以我们需要将<code>ctx =&gt; Route</code>转换为<code>Route</code>。而将<code>tupleValue</code>作为参数调用<code>route</code>后将获取结果类型<code>Future[RouteResult]</code>，这段代码的类型就是<code>inner =&gt; ctx =&gt; Future[RouteResult]</code> -&gt; <code>inner =&gt; Route</code>。</li>
</ol>
</div>
<div>
<a href="https://github.com/yangbajing/scala-web-development/tree/master/book/src/main/paradox/routing-dsl/custom-directive.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
1.0.0
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../routing-dsl/directive.html" title="Directive 指令" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
Directive 指令
</span>
</div>
</a>
<a href="../routing-dsl/rejections.html" title="拒绝 rejections" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
拒绝 rejections
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>

</div>
<div class="md-footer-social">
<a href="https://github.com/yangbajing" class="md-footer-social__link fa fa-github"></a><a href="https://weibo.com/yangbajing" class="md-footer-social__link fa fa-globe"></a><a href="https://www.yangbajing.me/" class="md-footer-social__link fa fa-globe"></a>
</div>

</div>
</div>
</footer>

</div>
<script src="../assets/javascripts/application.583bbe55.js"></script>
<script src="../assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"../."}})</script>
<script type="text/javascript" src="../lib/prettify/prettify.js"></script>
<script type="text/javascript" src="../lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>
